/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poo.biblioteca.vistas.libros;

import biblioteca.entidades.Autor;
import biblioteca.entidades.Editorial;
import biblioteca.entidades.Genero;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionListener;
import poo.biblioteca.vistas.autores.LsmAutor;

/**
 *
 * @author apo
 */
public class PnlLibro extends javax.swing.JPanel {

    private File archivoSeleccionado;

    /**
     * Creates new form PnlLibro
     */
    public PnlLibro() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtIdLibro = new javax.swing.JTextField();
        txtISBN = new javax.swing.JTextField();
        txtTitulo = new javax.swing.JTextField();
        spnEdicion = new javax.swing.JSpinner();
        txtDireccionPortada = new javax.swing.JTextField();
        btnBuscarPortada = new javax.swing.JButton();
        lblPortada = new javax.swing.JLabel();
        lblIdLibro = new javax.swing.JLabel();
        lblISBN = new javax.swing.JLabel();
        lblTitulo = new javax.swing.JLabel();
        lblEdicion = new javax.swing.JLabel();
        lblDireccionPortada = new javax.swing.JLabel();
        scpAutores = new javax.swing.JScrollPane();
        lstAutores = new javax.swing.JList();
        lblAutores = new javax.swing.JLabel();
        btnAgregarAutor = new javax.swing.JButton();
        cmbGeneros = new javax.swing.JComboBox();
        lblGenero = new javax.swing.JLabel();
        cmbEditorial = new javax.swing.JComboBox();
        lblEditorial = new javax.swing.JLabel();
        btnEliminarAutor = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de Libro"));

        txtIdLibro.setEnabled(false);

        txtISBN.setToolTipText("ISBN 9 o ISBN 13");

        spnEdicion.setModel(new javax.swing.SpinnerNumberModel(1, 1, 100, 1));

        txtDireccionPortada.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        txtDireccionPortada.setEnabled(false);

        btnBuscarPortada.setText("...");

        lblPortada.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblPortada.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lblIdLibro.setText("Id Libro:");

        lblISBN.setText("ISBN:");

        lblTitulo.setText("Título:");

        lblEdicion.setText("Edición:");

        lblDireccionPortada.setText("Portada:");

        lstAutores.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        scpAutores.setViewportView(lstAutores);

        lblAutores.setText("Autores:");

        btnAgregarAutor.setText("Buscar y Agregar Autor");

        lblGenero.setText("Genero:");

        lblEditorial.setText("Editorial:");

        btnEliminarAutor.setText("Eliminar Autor");
        btnEliminarAutor.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblIdLibro)
                    .addComponent(lblISBN)
                    .addComponent(lblTitulo)
                    .addComponent(lblEdicion)
                    .addComponent(lblDireccionPortada)
                    .addComponent(lblAutores)
                    .addComponent(lblGenero))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtDireccionPortada, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnBuscarPortada, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtTitulo, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(spnEdicion, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblEditorial)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmbEditorial, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtISBN, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtIdLibro, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cmbGeneros, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addGap(10, 10, 10)
                        .addComponent(lblPortada, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(scpAutores)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAgregarAutor, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEliminarAutor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtIdLibro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblIdLibro))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtISBN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblISBN))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblTitulo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(spnEdicion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblEdicion)
                            .addComponent(lblEditorial)
                            .addComponent(cmbEditorial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDireccionPortada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnBuscarPortada)
                            .addComponent(lblDireccionPortada))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbGeneros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblGenero)))
                    .addComponent(lblPortada, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scpAutores, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblAutores))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAgregarAutor)
                    .addComponent(btnEliminarAutor))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarAutor;
    private javax.swing.JButton btnBuscarPortada;
    private javax.swing.JButton btnEliminarAutor;
    private javax.swing.JComboBox cmbEditorial;
    private javax.swing.JComboBox cmbGeneros;
    private javax.swing.JLabel lblAutores;
    private javax.swing.JLabel lblDireccionPortada;
    private javax.swing.JLabel lblEdicion;
    private javax.swing.JLabel lblEditorial;
    private javax.swing.JLabel lblGenero;
    private javax.swing.JLabel lblISBN;
    private javax.swing.JLabel lblIdLibro;
    private javax.swing.JLabel lblPortada;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JList lstAutores;
    private javax.swing.JScrollPane scpAutores;
    private javax.swing.JSpinner spnEdicion;
    private javax.swing.JTextField txtDireccionPortada;
    private javax.swing.JTextField txtISBN;
    private javax.swing.JTextField txtIdLibro;
    private javax.swing.JTextField txtTitulo;
    // End of variables declaration//GEN-END:variables

    public void setIdLibro(String idLibro) {
        txtIdLibro.setText(idLibro);
    }

    public String getIdLibro() {
        return txtIdLibro.getText();
    }

    public void setISBN(String isbn) {
        txtISBN.setText(isbn);
    }

    public String getISBN() {
        return txtISBN.getText();
    }

    public void setTitulo(String titulo) {
        txtTitulo.setText(titulo);
    }

    public String getTitulo() {
        return txtTitulo.getText();
    }

    public void setEdicion(String edicion) {
        spnEdicion.setValue(Integer.parseInt(edicion));
    }

    public String getEdicion() {
        return spnEdicion.getValue().toString();
    }

    public void setDireccionPortada(String direccionPortada) {
        txtDireccionPortada.setText(direccionPortada);
    }

    public void setPortada(ImageIcon imageIcon) {
        lblPortada.setIcon(imageIcon);
    }

    public JLabel getLabelPortada() {
        return lblPortada;
    }
    
    public ImageIcon getPortada() {
        return (ImageIcon) lblPortada.getIcon();
    }

    public void setModeloListaAutores(ListModel<Autor> modelo) {
        lstAutores.setModel(modelo);
    }

    public ArrayList<Autor> getAutores() {
        return ((LsmAutor) lstAutores.getModel()).getAutores();
    }

    public void agregarAutorALista(Autor autor) {
        ((LsmAutor) lstAutores.getModel()).addAutor(autor);
        lstAutores.clearSelection();
    }

    public void btnAgregarAutorAddActionListener(ActionListener actionListener) {
        btnAgregarAutor.addActionListener(actionListener);

    }

    public void btnBuscarPortadaAddActionListener(ActionListener actionListener) {
        btnBuscarPortada.addActionListener(actionListener);
    }

    public void btnEliminarAutorAddActionListener(ActionListener actionListener) {
        btnEliminarAutor.addActionListener(actionListener);
    }

    public File mostrarDialogoBusquedaDePortada() {
        JFileChooser jfc = new JFileChooser();

        if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

            txtDireccionPortada.setText(jfc.getSelectedFile().getAbsolutePath());

        }

        archivoSeleccionado = jfc.getSelectedFile();
        return archivoSeleccionado;
    }

    public Editorial getEditorial() {
        return (Editorial) cmbEditorial.getSelectedItem();
    }

    public Genero getGenero() {
        return (Genero) cmbGeneros.getSelectedItem();
    }

    public void setEditoriales(ArrayList<Editorial> editoriales) {
        for (Editorial editorial : editoriales) {
            cmbEditorial.addItem(editorial);
        }
    }

    public void setGeneros(ArrayList<Genero> generos) {
        for (Genero genero : generos) {
            cmbGeneros.addItem(genero);
        }
    }

    public File getArchivoSeleccionado() {
        return archivoSeleccionado;
    }

    public void habilitarEliminarAutor(boolean habilitado) {
        btnEliminarAutor.setEnabled(habilitado);
    }

    public void lstAutoresAddListSelectionListener(ListSelectionListener listSelectionListener) {
        lstAutores.addListSelectionListener(listSelectionListener);

    }

    public void eliminarAutorSeleccionado() {
        ((LsmAutor) lstAutores.getModel()).eliminarAutor((Autor) lstAutores.getSelectedValue());
        if (((LsmAutor) lstAutores.getModel()).getAutores().isEmpty()) {
            btnEliminarAutor.setEnabled(false);
        }
    }

    public int getIndiceAutorSeleccionado() {
        return lstAutores.getSelectedIndex();
    }

    public void setGenero(Genero genero) {
        for (int i = 0; i < cmbGeneros.getItemCount(); i++) {
            if (((Genero) cmbGeneros.getItemAt(i)).getIdGenero() == genero.getIdGenero()) {
                cmbGeneros.setSelectedIndex(i);
                return;
            }

        }
    }

    public void setEditorial(Editorial editorial) {
        for (int i = 0; i < cmbEditorial.getItemCount(); i++) {
            if (((Editorial) cmbEditorial.getItemAt(i)).getIdEditorial() == editorial.getIdEditorial()) {
                cmbEditorial.setSelectedIndex(i);
                return;
            }

        }
    }

    public void setAutores(ArrayList<Autor> autores) {
        for (Autor autor : autores) {
            agregarAutorALista(autor);
        }
    }
}
